package subsets78;

import java.util.ArrayList;
import java.util.List;

public class Subsets {
    public List<List<Integer>> subsets(int[] nums) {
        //思路：回溯算法，注意顺序 不能重复
        List<List<Integer>> res = new ArrayList<>();
        backTrace(nums,0,new ArrayList<Integer>(),res);
        return res;
    }

    public void backTrace(int[] nums,int start,List<Integer> path,List<List<Integer>> res){
        res.add(new ArrayList<Integer>(path));//包含了空集合，每次递归把上次临时放入的结果放入结果集
        for (int i = start; i < nums.length; i++) {
            path.add(nums[i]);
            backTrace(nums,i+1,path,res);
            path.remove(path.size()-1);
        }
    }

    public static void main(String[] args) {

        Subsets subsets = new Subsets();
        int[] a = {1, 2, 3};
        System.out.println(subsets.subsets(a));
    }

}
